<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div随鼠标移动</title>
    <style>
        body{
            width: 2000px;
            height: 2000px;
        }
        #box1{
            width: 100px;
            height: 100px;
            background-color: aqua;
            position: absolute;
            top: 0;
            left: 0;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            let box1 = document.getElementById("box1");
            document.body.onmousemove = function (event) {
                event = event || window.event;
                //这里存在的问题是，当滚动条向下拉是，鼠标对应的原点和div对应的原点不一样，这个距离是滚动条的高度scrollTop
                //谷歌和其它浏览器不一直需要兼容
                let sy = document.body.scrollTop || document.documentElement.scrollTop;
                let sx = document.body.scrollLeft || document.documentElement.scrollLeft;
                let x = event.clientX;
                let y = event.clientY;
                box1.style.top = y+sy +'px';
                box1.style.left = x+sx +'px';
            }
        }
    </script>
</head>
<body>
<div id="box1"></div>
</body>
</html>